//
//  SouSuo.m
//  极致荟
//
//  Created by hetao on 14-9-12.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "SouSuo.h"
#import "SouSuoCell.h"
@implementation SouSuo

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initPoint:(CGPoint)point superView:(UIView *)sview concorell:(UIViewController *)controller
{
    SouSuo *ss=[[NSBundle mainBundle] loadNibNamed:@"SouSuo" owner:self options:nil][0];
    ss.superView=sview;
    ss.cont=controller;
    [ss setFrame:CGRectMake(point.x, point.y, ss.frame.size.width, ss.frame.size.height)];
    [ss.superView addSubview:ss];
    [ss.soutext setReturnKeyType:UIReturnKeySearch];
    ss.but.hidden=YES;
    [[NSNotificationCenter defaultCenter] addObserver:ss selector:@selector(notkeys:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    return ss;
}

///设置搜索结果数据，用于结果跟新
- (void)tableData:(NSArray *)arr
{
   
    tabledataarr=arr;
    [_table reloadData];
}

- (void)notkeys:(NSNotification *)not
{
   if(!self.soutext.isFirstResponder)
   {
       return;
   }
    CGPoint bpoint=[[[not userInfo] objectForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue];
    CGPoint epoint=[[[not userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];
    if(!_table)
    {
        _table=[[UITableView alloc] init];
        NSLog(@"%f ",_table.frame.size.height);
        [_table setDataSource:self];
        [_table setDelegate:self];
        [[UIApplication sharedApplication].keyWindow addSubview:_table];
        
    }
    CGPoint point= [self.superView  convertRect:self.frame fromView:self.cont.view].origin;
    if(bpoint.y>epoint.y)
    {
       
        self.but.hidden=NO;
        _table.hidden=NO;
     
         [_table setFrame: CGRectMake(0, point.y+self.frame.size.height+128, [UIScreen mainScreen].bounds.size.width, epoint.y-(point.y+self.frame.size.height))];
    }
    
}

#pragma mark-------TableView代理----------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

{
    return tabledataarr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *sell=@"SouSuoCell";
    SouSuoCell *cell=[tableView dequeueReusableCellWithIdentifier:sell];
    if(!cell)
    {
        cell=[[NSBundle mainBundle] loadNibNamed:@"SouSuoCell" owner:self options:nil][0];
    }
    [cell addCellData:tabledataarr[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self quxiao:nil];
    [self.souDelegate selectTableRow:tabledataarr[indexPath.row]];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    [self.souDelegate SouSuoText:textField.text];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.souDelegate SouSuoKeyRetunText:textField.text];
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    return YES;
}
- (IBAction)quxiao:(id)sender
{

    _table.hidden=YES;
    self.but.hidden=YES;
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}
@end
